<?php

namespace mobile\controllers;

use backend\models\AdminUser;
use common\models\UserEvalu;
use common\service\BaseToolService;
use mobile\models\form\FeedForm;
use mobile\service\EvaluService;
use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;

/**
 * 主页
 */
class SiteController extends Controller{

    /**
     * 反馈问题
     */
    public function actionIndex() {
//        $cookies = Yii::$app->request->cookies;//注意此处是request
//        $uuid = $cookies->get('useruuid', null);//设置默认值
//        if(empty($uuid)){
//            var_dump($uuid);
//            $uuid = BaseToolService::createGuid() ;
//            $time = 3600*24*7 ;
//            $cookies = Yii::$app->response->cookies;
//            $cookies->add(new \yii\web\Cookie([
//                'name' => 'useruuid',
//                'value' => $uuid,
//                'expire'=>time()+$time
//            ]));
//        }
        $model = new FeedForm();
        $msg = "" ;
        if (Yii::$app->request->isPost) {
            if($model->load(Yii::$app->request->post()) && $model->validate()){
                $model->pic_url1  = UploadedFile::getInstance($model, 'pic_url1');
                $model->pic_url2  = UploadedFile::getInstance($model, 'pic_url2');
                $model->pic_url3  = UploadedFile::getInstance($model, 'pic_url3');
                $model->video_url = UploadedFile::getInstance($model, 'video_url');
                $rs = $model->add() ;
                if($rs['code']==200){
                    return $this->redirect(["/tip/index","msg"=>" 已收到您的反馈，稍后会有专员联系您"]) ;
                }else{
                    $msg = $rs['msg'] ;
                }
            }else{
                $msg = current($model->getFirstErrors()) ;
            }
        }
        return $this->render('index',["model"=>$model,"msg"=>$msg]);
    }

    /**
     * 评价
     */
    public function actionEvalu(){
        $id = Yii::$app->request->get("id",0) ;
        if(empty($id)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作1"]) ;
        }

        $userId = Yii::$app->request->get("user_id",0) ;
        if(empty($userId)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作2"]) ;
        }

        $user = AdminUser::findOne(["user_id"=>$userId]) ;

        if(empty($user)){
            return $this->redirect(["/tip/index","msg"=>"用户不存在"]) ;
        }

        $time = Yii::$app->request->get("time",0) ;
        if(empty($time)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作3"]) ;
        }

        $useruuid = empty($_COOKIE['useruuid'])?null:$_COOKIE['useruuid'] ;//设置默认值
        if(!empty($useruuid)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作~"]) ;
        }

//        if(Yii::$app->admin->isGuest==false){
//            return $this->redirect(["/tip/index","msg"=>"请不要乱操作4"]) ;
//        }

        $session = Yii::$app->session;
        $user_id = $session->get('user_id') ;
        if(!empty($user_id)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作~"]) ;
        }

        $userEvalu = UserEvalu::findOne(["feed_id"=>$id,"user_id"=>$userId]) ;
        if(!empty($userEvalu)){
            $updTime = date("Y-m-d",strtotime($userEvalu['upd_time'])) ;
            if($updTime==date("Y-m-d")){
                return $this->redirect(["/tip/index","msg"=>"今天已经评价过无法再评价"]) ;
            }
        }

        $curtime = strtotime("now") ;

        $diff = 5*60 ; //15分钟有效
        if($curtime-$time>$diff){
            return $this->redirect(["/tip/index","msg"=>"二维码已失效"]) ;
        }

        $list = EvaluService::findEvaluList() ;
        return $this->render('evalu',["list"=>$list]);
    }

    /**
     * 添加评价
     */
    public function actionAddEvalu(){
        $id = Yii::$app->request->get("id",0) ;
        if(empty($id)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作1"]) ;
        }

        $userId = Yii::$app->request->get("user_id",0) ;
        if(empty($userId)){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作2"]) ;
        }

        $user = AdminUser::findOne(["user_id"=>$userId]) ;

        if(empty($user)){
            return $this->redirect(["/tip/index","msg"=>"用户不存在"]) ;
        }

        $time = Yii::$app->request->get("time",0) ;
        if(empty($time)){
            return $this->redirect(["/tip/index","msg"=>"非法操作"]) ;
        }

        $curtime = strtotime("now") ;

        $diff = 5*60 ; //15分钟有效
        if($curtime-$time>$diff){
            return $this->redirect(["/tip/index","msg"=>"二维码已失效"]) ;
        }

        if(!Yii::$app->request->isPost) {
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作3"]) ;
        }

        $userEvalu = UserEvalu::findOne(["feed_id"=>$id,"user_id"=>$userId]) ;
        if(!empty($userEvalu)){
            $updTime = date("Y-m-d",strtotime($userEvalu['upd_time'])) ;
            if($updTime==date("Y-m-d")){
                return $this->redirect(["/tip/index","msg"=>"今天已经评价过无法再评价"]) ;
            }
        }

        $arr = Yii::$app->request->post() ;
        $idArr = $arr["idArr"] ;
        if(count($idArr)<=0){
            return $this->redirect(["/tip/index","msg"=>"请不要乱操作4"]) ;
        }

        $content = "" ;
        foreach($idArr as $val){
            $title = $arr["title".$val] ;
            $evalu = empty($arr["select".$val])?"":$arr["select".$val] ;
            $content .= $title .":". $evalu ."|" ;
        }

        if(!empty($content)){
            $content = mb_substr($content,0,-1) ;
        }

        $userEvalu = new UserEvalu() ;

        $userEvalu->feed_id = $id ;
        $userEvalu->user_id = $userId ;
        $userEvalu->user_name = $user['username'] ;
        $userEvalu->feed = $content ;
        $userEvalu->upd_time = date("Y-m-d H:i:s") ;
        $rs = $userEvalu->save() ;

        if($rs==true){
            return $this->redirect(["/tip/index","msg"=>"感谢您的评价"]) ;
        }else{
            $msg = current($userEvalu->getFirstErrors()) ;
            return $this->redirect(["/tip/index","msg"=> "失败-".$msg]) ;
        }
    }


}
